﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using WebStore.Web.App.Models.Cart;
using WebStore.Web.App.Utility.Other;

namespace WebStore.Web.App.Controllers
{
    public partial class AdminController
    {
        private const int PageSize = 2;
        public ViewResult Orders(int page = 1)
        {
            var model = new OrdersModel
                            {
                                PagingInfo = new PagingInfo
                                                 {
                                                     CurrentPage = page,
                                                     ItemsPerPage = PageSize,
                                                     TotalItems = _entities.Orders.Count()
                                                 },
                                Orders = _entities.Orders.OrderBy(x=>x.Id).Skip((page - 1)*PageSize).Take(PageSize).ToList()
                                    .Select(Mapper.Map<OrderModel>).ToList()

                            };
            return View(model);
        }

        public ViewResult Order(int id)
        {
            return View(Mapper.Map<OrderModel>(_entities.Orders.FirstOrDefault(x=>x.Id==id)));
        }

        public ActionResult RejectOrder(int id)
        {
            _adminService.RejectOrder(id,CurrentUser.Id);
            return RedirectToAction("Orders");
        }

        public ActionResult RealizeOrder(int id)
        {
            _adminService.RealizeOrder(id,CurrentUser.Id);
            return RedirectToAction("Orders");
        }
    }
}